package sk.stuba.fiit.repka.dp.owl.gui;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import sk.stuba.fiit.repka.dp.owl.DataPropIndiAssociation;
import sk.stuba.fiit.repka.dp.owl.MyOWLIndividual;
import sk.stuba.fiit.repka.dp.owl.ObjPropIndiAssociation;

public class IndividualPanel extends JPanel {
	private JTable objPropTable;
	private JTable dataPropTable;
	private DefaultMutableTreeNode rootNode;
	private DefaultTreeModel treeModel;
	private JTree individualsTree;
	private JPanel individualTypePanel;
	private JLabel showNameLB;
	private JLabel showTypeLB;
	private JPanel objectPropertyPanel;
	private JScrollPane objectPropertySP;
	private JPanel dataPropertyPanel;
	private JScrollPane dataPropertySP;
	private MainPanel parent;

	/**
	 * Create the panel.
	 */
	public IndividualPanel(MainPanel parent) {
		this.parent = parent;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{161, 0, 0};
		gridBagLayout.rowHeights = new int[]{49, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JPanel individualListPanel = new JPanel();
		GridBagConstraints gbc_individualListPanel = new GridBagConstraints();
		gbc_individualListPanel.insets = new Insets(0, 0, 0, 5);
		gbc_individualListPanel.gridheight = 2;
		gbc_individualListPanel.fill = GridBagConstraints.BOTH;
		gbc_individualListPanel.gridx = 0;
		gbc_individualListPanel.gridy = 0;
		add(individualListPanel, gbc_individualListPanel);
		individualListPanel.setLayout(new GridLayout(1, 0, 0, 0));
		
		JScrollPane individualsSP = new JScrollPane();
		individualsSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		individualsSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		individualListPanel.add(individualsSP);
		
		this.rootNode = new DefaultMutableTreeNode("Individuals");
		this.treeModel = new DefaultTreeModel(this.rootNode);
		individualsTree = new JTree(treeModel);
		individualsTree.setEditable(false);
		individualsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		individualsTree.setShowsRootHandles(true);
		individualsSP.setViewportView(individualsTree);
		individualsTree.addMouseListener(new TreeMouseListener(this));
		this.createTreeNodes(rootNode);
		
		JPanel individualHeaderPanel = new JPanel();
		GridBagConstraints gbc_individualHeaderPanel = new GridBagConstraints();
		gbc_individualHeaderPanel.insets = new Insets(0, 0, 5, 0);
		gbc_individualHeaderPanel.fill = GridBagConstraints.BOTH;
		gbc_individualHeaderPanel.gridx = 1;
		gbc_individualHeaderPanel.gridy = 0;
		add(individualHeaderPanel, gbc_individualHeaderPanel);
		individualHeaderPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		
		JLabel individualLB = new JLabel("Individual");
		individualLB.setFont(new Font("Tahoma", Font.BOLD, 18));
		individualHeaderPanel.add(individualLB);
		
		JButton createIndividualBT = new JButton("Create New");
		createIndividualBT.addActionListener(new CreateNewBTActionListener(this));
		individualHeaderPanel.add(createIndividualBT);
		
		JButton deleteIndividualBT = new JButton("Delete");
		deleteIndividualBT.addActionListener(new DeleteBTActionListener(this));
		individualHeaderPanel.add(deleteIndividualBT);
		
		JPanel mergeTypePropertyPanel = new JPanel();
		GridBagConstraints gbc_mergeTypePropertyPanel = new GridBagConstraints();
		gbc_mergeTypePropertyPanel.fill = GridBagConstraints.BOTH;
		gbc_mergeTypePropertyPanel.gridx = 1;
		gbc_mergeTypePropertyPanel.gridy = 1;
		add(mergeTypePropertyPanel, gbc_mergeTypePropertyPanel);
		GridBagLayout gbl_mergeTypePropertyPanel = new GridBagLayout();
		gbl_mergeTypePropertyPanel.columnWidths = new int[]{540, 0};
		gbl_mergeTypePropertyPanel.rowHeights = new int[]{41, 140, 0};
		gbl_mergeTypePropertyPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_mergeTypePropertyPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		mergeTypePropertyPanel.setLayout(gbl_mergeTypePropertyPanel);
		
		individualTypePanel = new JPanel();
		GridBagConstraints gbc_individualTypePanel = new GridBagConstraints();
		gbc_individualTypePanel.fill = GridBagConstraints.BOTH;
		gbc_individualTypePanel.insets = new Insets(0, 0, 5, 0);
		gbc_individualTypePanel.gridx = 0;
		gbc_individualTypePanel.gridy = 0;
		mergeTypePropertyPanel.add(individualTypePanel, gbc_individualTypePanel);
		individualTypePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
		
		JLabel nameLB = new JLabel("Name: ");
		nameLB.setFont(new Font("Tahoma", Font.PLAIN, 14));
		individualTypePanel.add(nameLB);
		
		showNameLB = new JLabel("");
		showNameLB.setFont(new Font("Tahoma", Font.PLAIN, 14));
		individualTypePanel.add(showNameLB);
		
		JLabel typeLB = new JLabel("Type: ");
		typeLB.setFont(new Font("Tahoma", Font.PLAIN, 14));
		individualTypePanel.add(typeLB);
		
		showTypeLB = new JLabel("");
		showTypeLB.setFont(new Font("Tahoma", Font.PLAIN, 14));
		individualTypePanel.add(showTypeLB);
		
		JPanel individualPropertyPanel = new JPanel();
		GridBagConstraints gbc_individualPropertyPanel = new GridBagConstraints();
		gbc_individualPropertyPanel.fill = GridBagConstraints.BOTH;
		gbc_individualPropertyPanel.gridx = 0;
		gbc_individualPropertyPanel.gridy = 1;
		mergeTypePropertyPanel.add(individualPropertyPanel, gbc_individualPropertyPanel);
		individualPropertyPanel.setLayout(new GridLayout(1, 0, 0, 0));
		
		objectPropertyPanel = new JPanel();
		individualPropertyPanel.add(objectPropertyPanel);
		GridBagLayout gbl_objectPropertyPanel = new GridBagLayout();
		gbl_objectPropertyPanel.columnWidths = new int[]{108, 0, 0};
		gbl_objectPropertyPanel.rowHeights = new int[]{219, 13, 0, 0};
		gbl_objectPropertyPanel.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_objectPropertyPanel.rowWeights = new double[]{1.0, 1.0, 0.0, Double.MIN_VALUE};
		objectPropertyPanel.setLayout(gbl_objectPropertyPanel);
		
		this.initializeObjPropTable(null); //create table with object properties
		
		dataPropertyPanel = new JPanel();
		individualPropertyPanel.add(dataPropertyPanel);
		GridBagLayout gbl_dataPropertyPanel = new GridBagLayout();
		gbl_dataPropertyPanel.columnWidths = new int[]{0, 0, 0};
		gbl_dataPropertyPanel.rowHeights = new int[]{215, 12, 0, 0};
		gbl_dataPropertyPanel.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_dataPropertyPanel.rowWeights = new double[]{1.0, 1.0, 0.0, Double.MIN_VALUE};
		dataPropertyPanel.setLayout(gbl_dataPropertyPanel);
		
		this.initializeDataPropTable(null);

	}
	
	private void createTreeNodes(DefaultMutableTreeNode top) {
		DefaultMutableTreeNode individ = null;
		for(MyOWLIndividual in : this.parent.getIndividualsList()) {
			individ = new DefaultMutableTreeNode(in.getName());
			top.add(individ);
		}
	}
	
	//initialize objectPropertyPanel which has all object property informations
	//servers to refresh table with object properties
	private void initializeObjPropTable(MyOWLIndividual indi) {
		Object[][] rowsWithObjProp =new Object[][] {};
		
		if(indi != null) {
			if(indi.getObjAssociations().size() > 0) {
				rowsWithObjProp = new Object[indi.getObjAssociations().size()][2];
				int index = 0;
				for(ObjPropIndiAssociation asoc : indi.getObjAssociations()) {
					rowsWithObjProp[index][0] = asoc.getObjProperty().getName();
					rowsWithObjProp[index++][1] = asoc.getIndividual().getName();
				}
			} else {
				rowsWithObjProp =new Object[][] {};
			}
		} 
		
		objectPropertyPanel.removeAll();
		objectPropertySP = new JScrollPane();
		objectPropertySP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		objectPropertySP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_objectPropertySP = new GridBagConstraints();
		gbc_objectPropertySP.gridwidth = 2;
		gbc_objectPropertySP.insets = new Insets(0, 0, 5, 0);
		gbc_objectPropertySP.fill = GridBagConstraints.BOTH;
		gbc_objectPropertySP.gridx = 0;
		gbc_objectPropertySP.gridy = 0;
		objectPropertyPanel.add(objectPropertySP, gbc_objectPropertySP);
		
		objPropTable = new JTable();
		objPropTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		objPropTable.setModel(new DefaultTableModel(
			rowsWithObjProp,
			new String[] {
				"Object Property", "Individual"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		objectPropertySP.setViewportView(objPropTable);
		
		JPanel objButtonPanel = new JPanel();
		FlowLayout fl_objButtonPanel = (FlowLayout) objButtonPanel.getLayout();
		fl_objButtonPanel.setHgap(10);
		GridBagConstraints gbc_objButtonPanel = new GridBagConstraints();
		gbc_objButtonPanel.gridheight = 2;
		gbc_objButtonPanel.gridwidth = 2;
		gbc_objButtonPanel.insets = new Insets(0, 0, 5, 5);
		gbc_objButtonPanel.fill = GridBagConstraints.BOTH;
		gbc_objButtonPanel.gridx = 0;
		gbc_objButtonPanel.gridy = 1;
		objectPropertyPanel.add(objButtonPanel, gbc_objButtonPanel);
		
		JButton addObjPropBT = new JButton("Add");
		addObjPropBT.addActionListener(new AddObjPropBTActionListener(this));
		objButtonPanel.add(addObjPropBT);
		
		JButton removeObjPropBT = new JButton("Remove");
		removeObjPropBT.addActionListener(new RemoveObjPropBTActionListener(this));
		objButtonPanel.add(removeObjPropBT);
		
		this.objectPropertyPanel.repaint(); //refresh panel
		this.objectPropertyPanel.revalidate();
		this.objectPropertySP.repaint();
		this.objectPropertySP.revalidate();
	}
	
	//initialize dataPropertyPanel which has all data property informations
	//servers to refresh table with data properties
	private void initializeDataPropTable(MyOWLIndividual indi) {
		Object[][] rowsWithDataProp =new Object[][] {};
		
		if(indi != null) {
			if(indi.getDataAssociations().size() > 0) {
				rowsWithDataProp = new Object[indi.getDataAssociations().size()][2];
				int index = 0;
				for(DataPropIndiAssociation as : indi.getDataAssociations()) {
					rowsWithDataProp[index][0] = as.getDataProperty().getName();
					rowsWithDataProp[index++][1] = as.getValue();
				}
			} else {
				rowsWithDataProp =new Object[][] {};
			}
		} 
		this.dataPropertyPanel.removeAll();
		
		dataPropertySP = new JScrollPane();
		dataPropertySP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		dataPropertySP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_dataPropertySP = new GridBagConstraints();
		gbc_dataPropertySP.gridwidth = 2;
		gbc_dataPropertySP.insets = new Insets(0, 0, 5, 0);
		gbc_dataPropertySP.fill = GridBagConstraints.BOTH;
		gbc_dataPropertySP.gridx = 0;
		gbc_dataPropertySP.gridy = 0;
		dataPropertyPanel.add(dataPropertySP, gbc_dataPropertySP);
		
		dataPropTable = new JTable();
		dataPropTable.setModel(new DefaultTableModel(
			rowsWithDataProp,
			new String[] {
				"Data Property", "Value"
			}
		) {
			boolean[] columnEditables = new boolean[] {
					false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		
		dataPropertySP.setViewportView(dataPropTable);
		
		JPanel dataButtonPanel = new JPanel();
		FlowLayout fl_dataButtonPanel = (FlowLayout) dataButtonPanel.getLayout();
		fl_dataButtonPanel.setHgap(10);
		GridBagConstraints gbc_dataButtonPanel = new GridBagConstraints();
		gbc_dataButtonPanel.gridheight = 2;
		gbc_dataButtonPanel.gridwidth = 2;
		gbc_dataButtonPanel.insets = new Insets(0, 0, 5, 5);
		gbc_dataButtonPanel.fill = GridBagConstraints.BOTH;
		gbc_dataButtonPanel.gridx = 0;
		gbc_dataButtonPanel.gridy = 1;
		dataPropertyPanel.add(dataButtonPanel, gbc_dataButtonPanel);
		
		JButton addDataPropBT = new JButton("Add");
		addDataPropBT.addActionListener(new AddDataPropBTActionListener(this));
		dataButtonPanel.add(addDataPropBT);
		
		JButton removeDataPropBT = new JButton("Remove");
		removeDataPropBT.addActionListener(new RemoveDataPropBTActionListener(this));
		dataButtonPanel.add(removeDataPropBT);
	}
	
	private MyOWLIndividual findKlikedIndividualByName(String name) {
		MyOWLIndividual result = null;
		for(MyOWLIndividual in : this.parent.getIndividualsList()) {
			if(in.getName().equals(name)) {
				result = in;
				break;
			}
		}
		return result;
	}
	
	//method fill all screen commponents with selected individual attributes
	private void fillAtributesOfKlikedIndividual(MyOWLIndividual indi) {
		//show name and type of inidividual ... refresh individualTypePanel
		this.showNameLB.setText(indi.getName());
		this.showTypeLB.setText(indi.getType().getName());
		this.individualTypePanel.repaint();
		this.individualTypePanel.revalidate();
		
		//show table with object properties
		this.initializeObjPropTable(indi);
		//show table with data properties
		this.initializeDataPropTable(indi);
	}
	
	private void removeShownIndividual() {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		TreePath currentSelection = this.individualsTree.getSelectionPath();
		if (currentSelection != null) {
			DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection.getLastPathComponent());
			MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
			if (parent != null) {
				this.treeModel.removeNodeFromParent(currentNode); //remove from tree
				this.parent.getIndividualsList().remove(this.findKlikedIndividualByName(currentNode.getUserObject().toString())); //remove individual from list
			} else {
				// Either there was no selection, or the root was selected.
				toolkit.beep();
			}
		} else {
			// Either there was no selection, or the root was selected.
			toolkit.beep();
		}
	}
	
	private void removeSelectedObjectProperty(String propertyName) {
		ObjPropIndiAssociation toRemove = null;
		//find association in individual which have to be removed
		MyOWLIndividual indi = this.getCurrentSelectedIndividual();
		if(indi.getObjAssociations().size() > 0) {
			Iterator<ObjPropIndiAssociation> iter = indi.getObjAssociations().iterator();
			while(iter.hasNext()) {
				ObjPropIndiAssociation as = iter.next();
				if(as.getObjProperty().getName().equals(propertyName)) {
					toRemove = as;
				}
			}
		}
		indi.getObjAssociations().remove(toRemove);
		this.fillAtributesOfKlikedIndividual(indi); //refresh screen for this individual
	}
	
	private void removeSelectedDataProperty(String propertyName) {
		DataPropIndiAssociation toRemove = null;
		//find association in individual which have to be removed
		MyOWLIndividual indi = this.getCurrentSelectedIndividual();
		if(indi.getDataAssociations().size() > 0) {
			Iterator<DataPropIndiAssociation> iter = indi.getDataAssociations().iterator();
			while(iter.hasNext()) {
				DataPropIndiAssociation as = iter.next();
				if(as.getDataProperty().getName().equals(propertyName)) {
					toRemove = as;
				}
			}
		}
		indi.getDataAssociations().remove(toRemove);
		this.fillAtributesOfKlikedIndividual(indi); //refresh screen for this individual
	}
	
	private MyOWLIndividual getCurrentSelectedIndividual() {
		TreePath currentSelection = this.individualsTree.getSelectionPath();
		if (currentSelection != null) {
			DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection.getLastPathComponent());
			MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
			if (parent != null) {
				return this.findKlikedIndividualByName(currentNode.getUserObject().toString());
			}
		}
		return null;
	}
	
	private void addNewIndividual(MyOWLIndividual indi) {
		//add individual to tree
		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(indi.getName());
		this.treeModel.insertNodeInto(childNode, this.rootNode, this.rootNode.getChildCount());
		//set actually created note as selected
		individualsTree.setSelectionPath(new TreePath(childNode.getPath()));
		individualsTree.scrollPathToVisible(new TreePath(childNode.getPath()));
		//add individual to list
		this.parent.getIndividualsList().add(indi);
	}
	
	//method clear all shown attributes in panels of removed individual
	private void clearIndividualAttributes() {
		this.showNameLB.setText("");
		this.showTypeLB.setText("");
		this.individualTypePanel.repaint();
		this.individualTypePanel.revalidate();
	}
	
	// map object properties to current selected individual
	private void mapObjectPropToIndividual(ObjPropIndiAssociation association) {
		MyOWLIndividual indi = this.getCurrentSelectedIndividual();
		if (indi != null) {
			indi.getObjAssociations().add(association);
		}
	}
	
	//map data properties to current selected individual
	private void mapDataPropToIndividual(DataPropIndiAssociation association) {
		MyOWLIndividual indi = this.getCurrentSelectedIndividual();
		if (indi != null) {
			indi.getDataAssociations().add(association);
		}
	}
	
	private class CreateNewBTActionListener implements ActionListener {
		private IndividualPanel p;
		public CreateNewBTActionListener(IndividualPanel p) {
			this.p = p;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			CreateIndividualDialog dialog = new CreateIndividualDialog(p.parent.getOwlClassesList());
			if(dialog.getOutputName() != null && dialog.getOutputCLass() != null) {
				MyOWLIndividual indi = new MyOWLIndividual(dialog.getOutputName(), dialog.getOutputCLass());
				p.addNewIndividual(indi);
				p.fillAtributesOfKlikedIndividual(indi);	
			}
		}
		
	}
	
	private class DeleteBTActionListener implements ActionListener {
		private IndividualPanel p;
		public DeleteBTActionListener(IndividualPanel p) {
			this.p = p;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			p.removeShownIndividual();
			p.clearIndividualAttributes();
			
		}
		
	}
	
	private class AddObjPropBTActionListener implements ActionListener {
		private IndividualPanel p;
		public AddObjPropBTActionListener(IndividualPanel p) {
			this.p = p;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			if (!p.individualsTree.isSelectionEmpty()) {
				MyOWLIndividual indi = p.getCurrentSelectedIndividual();
				if(indi != null) {
//					SelectPropertyDialog dialog = new SelectPropertyDialog(p.parent.getObjectPropertyList(), "Select Object Property");
					AddObjectToIndividualDialog dialog = new AddObjectToIndividualDialog(p.parent.getObjectPropertyList(), p.parent.getIndividualsList());
					if(dialog.getOutput() != null) {
						p.mapObjectPropToIndividual(dialog.getOutput()); 
						p.fillAtributesOfKlikedIndividual(indi);
					}
				} else {
					toolkit.beep();
				}
				
			} else {
				toolkit.beep();
			}
		}
		
	}
	
	private class RemoveObjPropBTActionListener implements ActionListener {
		private IndividualPanel p;
		public RemoveObjPropBTActionListener(IndividualPanel p) {
			this.p = p;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			if(-1 != p.objPropTable.getSelectedRow()) {
				p.removeSelectedObjectProperty(p.objPropTable.getValueAt(p.objPropTable.getSelectedRow(), 0).toString());
			} else {
				toolkit.beep();
			}
		}
		
	}
	
	private class AddDataPropBTActionListener implements ActionListener {
		private IndividualPanel p;
		public AddDataPropBTActionListener(IndividualPanel p) {
			this.p = p;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			if (!p.individualsTree.isSelectionEmpty()) {
				MyOWLIndividual indi = p.getCurrentSelectedIndividual();
				if(indi != null) {
					AddDataToIndividualDialog dialog = new AddDataToIndividualDialog(p.parent.getDataPropertyList());
					if(dialog.getOutput() != null) {
						p.mapDataPropToIndividual(dialog.getOutput());
						p.fillAtributesOfKlikedIndividual(indi);
					}
				} else {
					toolkit.beep();
				}
				
			} else {
				toolkit.beep();
			}
		}
		
	}
	
	private class RemoveDataPropBTActionListener implements ActionListener {
		private IndividualPanel p;
		public RemoveDataPropBTActionListener(IndividualPanel p) {
			this.p = p;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			if(-1 != p.dataPropTable.getSelectedRow()) {
				p.removeSelectedDataProperty(p.dataPropTable.getValueAt(p.dataPropTable.getSelectedRow(), 0).toString());
			} else {
				toolkit.beep();
			}
		}
	}
	
	private class TreeMouseListener implements MouseListener {
		private IndividualPanel p;
		public TreeMouseListener(IndividualPanel p) {
			this.p = p;
		}
		
		@Override
		public void mouseClicked(MouseEvent e) {
			TreePath currentSelection = p.individualsTree.getPathForLocation(e.getX(), e.getY());
			if(currentSelection != null) {
				DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection.getLastPathComponent());
				MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
				if (parent != null) {
					MyOWLIndividual indi = p.findKlikedIndividualByName(currentNode.getUserObject().toString());
					p.fillAtributesOfKlikedIndividual(indi);
				}
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {}

		@Override
		public void mouseExited(MouseEvent arg0) {}

		@Override
		public void mousePressed(MouseEvent arg0) {}

		@Override
		public void mouseReleased(MouseEvent arg0) {}
		
	}
}
